//
//  LoginVC.m
//  YangWifi
//
//  Created by zuoxueliangduyuanzhuo on 2017/6/15.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "LoginVC.h"
#import "JKCountDownButton.h"
#import "Regex.h"
#import "NSString+Ext.h"
#import "LoginModel.h"
#import "RegistFirstVC.h"

@interface LoginVC ()<UITextFieldDelegate>

@property (nonatomic ,weak) IBOutlet UITextField *phoneTf;

@property (nonatomic ,weak) IBOutlet UITextField *pwdTf;

@property (nonatomic ,weak) IBOutlet UIButton *registBtn;

@property (nonatomic ,weak) IBOutlet UIButton *loginBtn;


@end

@implementation LoginVC

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"loginHandlerOrder" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"loginHandlerHome" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"登录";
    [self buildNavBackItemWithAction:@selector(backNavClick:)];
    [self buildNavRightItemWithTitle:@"忘记密码?" action:@selector(forgotPwdAction:)];
    self.registBtn.layer.cornerRadius = 2;
    self.registBtn.layer.masksToBounds = YES;
    self.registBtn.layer.borderColor = xlHexColor(@"#979797").CGColor;
    self.registBtn.layer.borderWidth = 1;
}

- (void)forgotPwdAction:(UIButton *)btn
{
    RegistFirstVC * vc = [[RegistFirstVC alloc] initWithNibName:@"RegistFirstVC" bundle:nil];
    vc.pwdType = ForgotPwdType;
    [self showViewController:vc sender:nil];
}

- (void)getUserInfo
{
    NSDictionary *params = @{
                             @"type": @"OSWIFI_USERINFO_MOBILE",
                             @"token":[AccountHanler token],
                             @"requestEntity": @{
                                     @"telephone":[AccountHanler telPhone],
                                     @"method":@"query",
                                     }
                             };
    [MHNetworkManager postReqeustWithURL:nil params:params successBlock:^(NSDictionary *returnData) {
        XLBaseModel *result = [XLBaseModel mj_objectWithKeyValues:returnData];
        if (result.resultFlag) {
            
        }else{
            DLog(@"用户信息获取失败");
        }
    } failureBlock:^(NSError *error) {
        DLog(@"用户信息获取失败");
    } showHUD:NO];
}


- (BOOL)regexLoginInfo
{
    if ([[NSString deleteForeAndAftSpace:self.phoneTf.text] length] == 0) {
        
        [self.view makeToast:@"请输入手机号码"];
        return NO;
    
    }else if (![Regex isValidatePhoneNumber:self.phoneTf.text]){
    
        [self.view makeToast:@"请输入正确的手机号码"];
        return NO;
   
    }else if ([[NSString deleteForeAndAftSpace:self.pwdTf.text] length] == 0){
       
        [self.view makeToast:@"请输入密码"];
        return NO;
    }
    
    return YES;
    
}

- (IBAction)wechatAction:(id)sender
{
    
}

- (IBAction)QQAction:(id)sender
{
    
}


- (IBAction)registAction:(id)sender
{
    RegistFirstVC * vc = [[RegistFirstVC alloc] initWithNibName:@"RegistFirstVC" bundle:nil];
    vc.pwdType = RegistPwdType;
    [self showViewController:vc sender:nil];
}

- (IBAction)loginAction:(id)sender
{
    [self.view endEditing:YES];
    if ([self regexLoginInfo]) {
        return;
    }
    
    NSDictionary *params = @{@"principals":@"sanguo",
                             @"passWord":@"123qwe",
//                             @"logType":@"2",
//                             @"imei":@"",
                             };
    @WeakObj(self);
    [MHNetworkManager postReqeustWithURL:@"login" params:params successBlock:^(NSDictionary *returnData) {
        XLBaseModel *data = [XLBaseModel mj_objectWithKeyValues:returnData];
        
        if (data.resultFlag) {
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [app.window makeToast:@"登录成功"];
            [AccountHanler saveIsLogin:YES];
            LoginModel *model = [LoginModel mj_objectWithKeyValues:data.responseEntity[0]];
            [AccountHanler saveToken:model.token];
            [AccountHanler saveTelPhone:Weakself.phoneTf.text];
            [AccountHanler saveNowUsePackage:model.nowUsePackage];
            
            [app getAllPackageList];
            
            if (self.loginHandler) {
                self.loginHandler();
            }
            
            if (self.isNeedOrderNoti) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"loginHandlerOrder" object:nil userInfo:nil];
            }
            
            if (self.isNeedHomeNoti) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"loginHandlerHome" object:nil userInfo:nil];
            }
        
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
            
        }else{
            
            [Weakself.view makeToast:@"登录失败"];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        [Weakself.view makeToast:@"登录失败"];
        
    } showHUD:YES];

    

}
- (void)backNavClick:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
